package cn.uppower.wordaliy.activity;


import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.blankj.utilcode.util.ActivityUtils;

import java.util.ArrayList;
import java.util.List;

import cn.uppower.wordaliy.R;
import cn.uppower.wordaliy.fragment.AboutMeFragment;
import cn.uppower.wordaliy.fragment.DakaFragment;
import cn.uppower.wordaliy.fragment.SouYeFragment;
import cn.uppower.wordaliy.fragment.WordsFragment;
import cn.uppower.wordaliy.widget.BaseActivity;

public class MainActivity extends BaseActivity {

    private static  final int SOUYE_FRAGMENT=111;
    private static  final int WORDS_FRAGMENT=222;
    private static  final int DAKA_FRAGMENT=333;
    private static  final int ABOUT_ME_FRAGMENT=444;
    private static final int WANGT_EXIT = 555;
    private int nowChecked =0;
    private List<Fragment> fragments;
    private SouYeFragment souYeFragment;
    private WordsFragment wordsFragment;
    private DakaFragment dakaFragment;
    private AboutMeFragment aboutMeFragment;

    private LinearLayout ll_shouye,ll_danci,ll_daka,ll_wode;

    private FragmentTransaction fragmentTransaction;


    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if (msg.what==WANGT_EXIT){
                isExit=false;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public Integer getLayoutResId() {
        return R.layout.activity_main;
    }

    @Override
    public void init() {
        ll_shouye = findViewById(R.id.ll_shouye);
        ll_danci = findViewById(R.id.ll_danci);
        ll_daka = findViewById(R.id.ll_daka);
        ll_wode = findViewById(R.id.ll_wode);
        ll_shouye.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkBar(SOUYE_FRAGMENT);
            }
        });

        ll_danci.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkBar(WORDS_FRAGMENT);
            }
        });

        ll_daka.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkBar(DAKA_FRAGMENT);
            }
        });

        ll_wode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkBar(ABOUT_ME_FRAGMENT);
            }
        });

        fragments=new ArrayList<>();
        checkBar(SOUYE_FRAGMENT);

    }

    private void checkBar(int i) {
        if (nowChecked==i){
            return;
        }
        setAllNotChecked();
        fragmentTransaction=getSupportFragmentManager().beginTransaction();
        hideAllFragment();

        switch (i){
            case SOUYE_FRAGMENT:
                ll_shouye.setSelected(true);
                if (souYeFragment==null){
                    souYeFragment=new SouYeFragment();
                    fragmentTransaction.add(R.id.fl_main,souYeFragment);
                }
                fragmentTransaction.show(souYeFragment);

                break;
            case WORDS_FRAGMENT:
                ll_danci.setSelected(true);
                if (wordsFragment==null){
                    wordsFragment=new WordsFragment();
                    fragmentTransaction.add(R.id.fl_main,wordsFragment);
                }
                fragmentTransaction.show(wordsFragment);
                break;
            case DAKA_FRAGMENT:
                ll_daka.setSelected(true);
                if (dakaFragment==null){
                    dakaFragment=new DakaFragment();
                    fragmentTransaction.add(R.id.fl_main,dakaFragment);
                }
                fragmentTransaction.show(dakaFragment);
                break;
            case ABOUT_ME_FRAGMENT:
                ll_wode.setSelected(true);
                if (aboutMeFragment==null){
                    aboutMeFragment=new AboutMeFragment();
                    fragmentTransaction.add(R.id.fl_main,aboutMeFragment);
                }
                fragmentTransaction.show(aboutMeFragment);
                break;
            default:
                Toast.makeText(this, "系统错误,请退出重新进入app", Toast.LENGTH_SHORT).show();
                break;
        }

        fragmentTransaction.commit();
        nowChecked=i;

    }

    private void hideAllFragment() {
        if (souYeFragment!=null){
            fragmentTransaction.hide(souYeFragment);
        }

        if (wordsFragment!=null){
            fragmentTransaction.hide(wordsFragment);
        }

        if (dakaFragment!=null){
            fragmentTransaction.hide(dakaFragment);
        }

        if (aboutMeFragment!=null){
            fragmentTransaction.hide(aboutMeFragment);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
//        结束之前的所有activity
        ActivityUtils.finishAllActivitiesExceptNewest();
    }

    private void setAllNotChecked() {
        ll_shouye.setSelected(false);
        ll_danci.setSelected(false);
        ll_daka.setSelected(false);
        ll_wode.setSelected(false);
    }


    private boolean isExit = false;
    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if (keyCode==KeyEvent.KEYCODE_BACK&&event.getAction()==KeyEvent.ACTION_UP){
            if (!isExit){
                isExit=true;
                Toast.makeText(this, "再按一次返回退出Wordaliy", Toast.LENGTH_SHORT).show();
                handler.sendEmptyMessageDelayed(WANGT_EXIT,2000);
                return true;
            }
        }
        return super.onKeyUp(keyCode, event);
    }
}
